package com.hyphenate.easeui.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.e.library.activity.EActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.map.GLocation;

/**
 * 谷歌定位
 * Created by e on 2017/8/15.
 * OnMapReadyCallback ,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener
 */

public class EaseShowGMapActivity extends EActivity implements OnMapReadyCallback{

    public static final String TAG = "GLocation";

    private GoogleMap mMap;
    private GLocation gLocation;

    @Override
    protected int layout() {
        return R.layout.activity_show_gmap;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        gLocation = new GLocation(this){
            @Override
            protected void successful(double lng, double lat) {
                super.successful(lng, lat);
            }

            @Override
            protected void failure() {
                super.failure();
            }
        };

        gLocation.setZoom(15);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        mMap.setTrafficEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setZoomGesturesEnabled(true);
        gLocation.onMapReady(mMap);

        Intent intent = getIntent();
        double lat = intent.getDoubleExtra("latitude",0);
        double lng = intent.getDoubleExtra("longitude",0);
        String address = intent.getStringExtra("address");
        LatLng latLng = new LatLng(lat, lng);
        mMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_location_marker)).title(address));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
    }

    @Override
    public void onStart() {
        super.onStart();
        gLocation.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        gLocation.onStop();
    }

    @Override
    public void onPause() {
        super.onPause();
        gLocation.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        gLocation.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        gLocation.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        gLocation.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }
}
